package com.tju.resource.oauth.controller;

import com.tju.resource.tool.result.Result;
import com.tju.resource.tool.result.ResultEnum;
import com.tju.resource.tool.result.ResultUtil;
import com.tju.resource.model.SysUser;
import com.tju.resource.tool.tool.ClientParameters;
import com.tju.resource.server.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 用户信息业务控制类
 */
@Slf4j
@RestController
public class SysUserController {
    private static final String prefix = "user";

    @Autowired
    private SysUserService sysUserService;

    /**
     * 新增用户信息
     *
     * @param sysUser
     * @param result
     * @return
     */
    @PostMapping(prefix + "/addUserInformation.do")
    @PreAuthorize("hasAuthority('sys:user:add')")
    public Result<Object> addUserInformation(@Valid @RequestBody SysUser sysUser, BindingResult result) {
        if (result.hasErrors()) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        } else
            return sysUserService.addUserInformation(sysUser);
    }

    /**
     * 删除用户信息
     *
     * @param sysUser
     * @return
     */
    @PostMapping(prefix + "/deleteUserInformation.do")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    public Result<Object> deleteUserInformation(@RequestBody SysUser sysUser) {
        return sysUserService.deleteUserInformation(sysUser);
    }

    /**
     * 修改用户信息(非修改密码)
     *
     * @param sysUser
     * @return
     */
    @PostMapping(prefix + "/modifyUserInformation.do")
    @PreAuthorize("hasAuthority('sys:user:edit')")
    public Result<Object> modifyUserInformation(@RequestBody SysUser sysUser) {
        return sysUserService.modifyUserInformation(sysUser);
    }

    /**
     * 修改用户密码
     *
     * @param sysUser
     * @return
     */
    @PostMapping(prefix + "/modifyUserPassword.do")
    public Result<Object> modifyUserPassword(@RequestBody SysUser sysUser) {
        return sysUserService.modifyUserPassword(sysUser);
    }

    /**
     * 查询全部用户信息
     *
     * @return
     */
    @PostMapping(prefix + "/getPageUser.do")
    @PreAuthorize("hasAnyAuthority('sys:user:list')")
    public Result<Object> getPageUser(@RequestBody ClientParameters parameter) {
        return sysUserService.getPageUser(parameter);
    }

    /**
     * 查询个人用户信息
     *
     * @return
     */
    @PostMapping(prefix + "/getPersonalInformation.do")
    @PreAuthorize("hasAnyAuthority('sys:user:list')")
    public Result<Object> getPersonalInformation(@RequestBody SysUser sysUser) {
        return sysUserService.getPersonalInformation(sysUser);
    }

}
